<template>
  <Form class="xtx-form" v-slot='{ errors }'>
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field :rules='schema.mobile' v-model='form.mobile' name='mobile' class="input" type="text" placeholder="绑定的手机号" />
      </div>
      <div class="error" v-if='errors.mobile'>{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field :rules='schema.code' v-model='form.code' name='code' class="input" type="text" placeholder="短信验证码" />
        <span @click='sendCode' class="code">发送验证码</span>
      </div>
      <div class="error" v-if='errors.code'>{{errors.code}}</div>
    </div>
    <a @click='handleBind' href="javascript:;" class="submit">立即绑定</a>
  </Form>
</template>

<script>
import { Form, Field } from 'vee-validate'
import schema from '@/utils/validate-schema.js'
import { reactive } from 'vue'
import { userQQBindCode, userQQBindLogin } from '@/api/user.js'
import Message from '@/components/library/Message.js'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

export default {
  name: 'CallbackBind',
  components: { Form, Field },
  props: {
    unionId: {
      type: String,
      required: true
    },
    nickname: {
      type: String,
      default: ''
    },
    avatar: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const router = useRouter()
    const store = useStore()
    const form = reactive({
      mobile: '',
      code: ''
    })
    // 发送验证码
    const sendCode = async () => {
      const isOk = schema.mobile(form.mobile)
      if (isOk === true) {
        try {
          // 手机号正常
          const ret = await userQQBindCode(form.mobile)
          if (ret && ret.msg === '操作成功') {
            Message({ type: 'success', text: '获取验证码成功' })
          } else {
            Message({ type: 'error', text: ret.message })
          }
        } catch {
          Message({ type: 'error', text: '获取验证码失败' })
        }
      }
    }
    // 绑定手机号提交表单
    const handleBind = async () => {
      try {
        const ret = await userQQBindLogin(props.unionId, form.mobile, form.code)
        if (ret && ret.result) {
          // 绑定成功，存储用户信息，跳转到主页
          store.commit('user/updateUser', ret.result)
          router.push('/')
        } else {
          Message({ type: 'error', text: '绑定手机号失败' })
        }
      } catch {
        Message({ type: 'error', text: '绑定手机号失败' })
      }
    }
    return { schema, form, sendCode, handleBind }
  }
}
</script>

<style scoped lang='less'>
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
